package com.wj.chatroom.controller;
import com.wj.chatroom.mapper.MessageMapper;
import com.wj.chatroom.mapper.MessageSessionMapper;
import com.wj.chatroom.model.Friend;
import com.wj.chatroom.model.MessageSession;
import com.wj.chatroom.model.MessageSessionUserItem;
import com.wj.chatroom.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@RestController
public class MessageSessionController {
    @Autowired
    private MessageSessionMapper messageSessionMapper;
    @Autowired
    private MessageMapper messageMapper;
    @RequestMapping("/sessionList")
    //获取会话信息
    public Object getMessageSessionList(HttpServletRequest req){
        List<MessageSession> messageSessionList = new ArrayList<>();
        HttpSession session = req.getSession(false);
        if(session==null){
            System.out.println("session为null,用户未登录");
            return messageSessionList;
        }
        User user = (User) session.getAttribute("user");
        if(user==null){
            System.out.println("user为空");
            return messageSessionList;
        }
        List<Integer> sessionIdList = messageSessionMapper.getSessionIdsByUserId(user.getUserId());
        for(int sessionId:sessionIdList){
            MessageSession messageSession = new MessageSession();
            messageSession.setSessionId(sessionId);
            List<Friend>friends = messageSessionMapper.getFriendsBySessionId(sessionId, user.getUserId());
            messageSession.setFriends(friends);
            String lastMessage = messageMapper.getLastMessageBySessionId(sessionId);
            if(lastMessage==null){
                messageSession.setLastMessage("");
            }else {
                messageSession.setLastMessage(lastMessage);
            }
            messageSessionList.add(messageSession);
        }
        return  messageSessionList;
    }
    @RequestMapping("/session")
    @Transactional //保证两次插入操作要么全部成功，要么全部回滚
    public Object addMessageSession(int toUserId, @SessionAttribute("user") User user){
        HashMap<String,Integer> resp = new HashMap<>();
        MessageSession messageSession = new MessageSession();
        //messageSession 里的friends 和lastMessage属性用不上，传这个参数主要是为了获取到会话的sessionId
        messageSessionMapper.addMessageSession(messageSession);
        //给message_session_user 表里插入记录
        MessageSessionUserItem item1 = new MessageSessionUserItem();
        item1.setSessionId(messageSession.getSessionId());
        item1.setUserId(user.getUserId());
        messageSessionMapper.addMessageSessionUser(item1);
        //给message_session_user 表里插入记录
        MessageSessionUserItem item2 = new MessageSessionUserItem();
        item2.setSessionId(messageSession.getSessionId());
        item2.setUserId(toUserId);
        messageSessionMapper.addMessageSessionUser(item2);
        //返回的对象是一个普通对象也行，或者是一个Map也行，json都能进行处理
        resp.put("sessionId",messageSession.getSessionId());
        return  resp;
    }
}